package com.yuven.appframework.widget.titlebar

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import com.yuven.appframework.R
import com.yuven.baselib.utils.ViewUtils

/**
 * 
 *
 * @author :        Yuven
 * @date :          2021/7/30 10:08 上午
 * @desc :
 */
open class BaseTitleBarFactory :ITitleBarFactory{
    override fun createTitleTextView(context:Context): TextView {
        val tvTitle: TextView = createTextView(context)
        tvTitle.layoutParams = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            Gravity.CENTER_HORIZONTAL
        )
        tvTitle.gravity = Gravity.CENTER
        tvTitle.compoundDrawablePadding =
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                2f,
                tvTitle.resources.displayMetrics
            )
                .toInt()
        tvTitle.setSingleLine()
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
        return tvTitle
    }

    override fun createTitleIcon(context: Context?): Drawable? {
        return null
    }

    override fun createLeftTextView(context:Context): TextView {
        val tvLeft = createTextView(context)
        tvLeft.setCompoundDrawablesWithIntrinsicBounds(createLeftIcon(context), null, null, null)
        tvLeft.layoutParams = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            Gravity.START
        )
        tvLeft.gravity = Gravity.CENTER_VERTICAL
        tvLeft.compoundDrawablePadding =
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                2f,
                tvLeft.resources.displayMetrics
            )
                .toInt()
        tvLeft.setSingleLine()
        tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
        return tvLeft
    }

    override fun createLeftIcon(context: Context): Drawable? {
        return getDrawableResources(context, R.drawable.core_title_back)
    }

    override fun createRightTextView(context:Context): TextView {
        val tvRight = createTextView(context)
        tvRight.setCompoundDrawablesWithIntrinsicBounds(createRightIcon(context), null, null, null)
        tvRight.layoutParams = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT,
            Gravity.END
        )
        tvRight.gravity = Gravity.CENTER_VERTICAL
        tvRight.compoundDrawablePadding =
            TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                2f,
                tvRight.resources.displayMetrics
            )
                .toInt()
        tvRight.setSingleLine()
        tvRight.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
        return tvRight
    }

    override fun createRightIcon(context: Context): Drawable? {
        return null
    }

    override fun getTitleBarHeight(): Int {
        return ViewUtils.dip2px(50f)
    }

    /**
     * 获取图片资源
     */
    private fun getDrawableResources(context: Context, id: Int): Drawable? {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            context.resources.getDrawable(id, context.theme)
        } else {
            context.resources.getDrawable(id)
        }
    }

    protected open fun createTextView(context: Context): TextView {
        return TextView(context)
    }
}